﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PControl
{
    /// <summary>
    /// 加载动画控件
    /// </summary>
    public partial class Loading : UserControl
    {
        /// <summary>
        /// 控件图片资源
        /// </summary>
        private static Image[] defaultImageList = {global::PControl.Properties.Resources._1,
                                        global::PControl.Properties.Resources._2,
                                        global::PControl.Properties.Resources._3,
                                        global::PControl.Properties.Resources._4,
                                        global::PControl.Properties.Resources._5,
                                        global::PControl.Properties.Resources._6,
                                        global::PControl.Properties.Resources._7,
                                        global::PControl.Properties.Resources._8};
        private Image[] backImageList = defaultImageList;
        /// <summary>
        /// 当前显示图片资源索引
        /// </summary>
        private int currentIndex=7;
        /// <summary>
        /// 控件是否处于加载状态
        /// </summary>
        private bool isLoading = false;
        /// <summary>
        /// 动画切换事件间隔
        /// </summary>
        private int timerInterval = 200;
        /// <summary>
        /// 改变控件大小前控件的尺寸
        /// </summary>
        private Size preSize=new Size(50,50);

        /// <summary>
        /// 获取或设置控件加载动画图片资源
        /// </summary>
        public Image[] BackImageList
        {
            get { return backImageList; }
            set
            {
                if (value != null)
                    backImageList = value;
                else backImageList = defaultImageList;
            }
        }
        /// <summary>
        /// 获取或设置动画更新时间间隔，规定取值范围50-3000，单位毫秒
        /// 过小，更新太快；太大，图片切换太慢，不流畅
        /// </summary>
        public int TimerInterval
        {
            get { return timerInterval; }
            set
            {
                timerInterval = value;
                if (timerInterval < 50)
                    timerInterval = 50;
                else if (timerInterval > 3000)
                    timerInterval = 3000;
                timer1.Interval = timerInterval;
            }
        }
        /// <summary>
        /// 默认构造方法
        /// </summary>
        public Loading()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 控件被加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Loading_Load(object sender, EventArgs e)
        {
            isLoading = true;
            timer1_Tick(sender, e);
        }
        /// <summary>
        /// 控件大小改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Loading_SizeChanged(object sender, EventArgs e)
        {
            if (preSize != this.Size)
            {
                if (this.Height == this.Width)
                {
                    pictureBox.Height = pictureBox.Width = this.Width;
                    pictureBox.Location = new Point(0, 0);
                }
                else if (this.Height > this.Width)
                {
                    pictureBox.Height = pictureBox.Width = this.Width;
                    pictureBox.Location = new Point(0, (this.Height - this.Width) / 2);
                }
                else
                {
                    pictureBox.Height = pictureBox.Width = this.Height;
                    pictureBox.Location = new Point((this.Width - this.Height) / 2, 0);
                }
                preSize = this.Size;
            }
        }
        /// <summary>
        /// 动画用计时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isLoading)
            {
                currentIndex++;
                if (currentIndex >= backImageList.Length)
                    currentIndex = 0;
                this.pictureBox.BackgroundImage = backImageList[currentIndex];
                //设置随机时间间隔
                //timer1.Interval = new Random().Next(250);
            }
        }
        /// <summary>
        /// 停止控件加载动画
        /// </summary>
        public void Stop()
        {
            timer1.Stop();
            isLoading = false;
        }
        /// <summary>
        /// 启动控件加载动画
        /// </summary>
        public void Start()
        {
            isLoading = true;
            timer1.Start();
        }
    }
}
